﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Adex;
using MediaMonitor.Common;
using Telerik.Web.UI;

namespace MediaMonitor.Admin
{
    public partial class Company_Mgmt_List : Adex.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserRole"] != null)
            {
                if (Session["UserRole"].ToString() == "advanced user")
                {
                    aAddNewClients.HRef = "javascript:void(0);";
                }
                else
                {
                    aAddNewClients.HRef = "company-mgmt-addedit.aspx";
                }
            }
            else
                Response.Redirect("../login.aspx");
        }

        protected void radGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
           
            DataTable dt = Company.Query().SetSelectList("*, CONVERT(VARCHAR, DateAdded, 101) AS LastModifiedDate, CONVERT(VARCHAR, ValidTo, 101) AS ValidToDate, CONVERT(VARCHAR, ValidFrom, 101) AS ValidFromDate").ORDER_BY("CompanyName").ExecuteDataSet().Tables[0];
            radGrid.DataSource = dt;

        }

        protected void radGrid_DeleteCommand(Object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            
                string pkId = radGrid.MasterTableView.DataKeyValues[e.Item.ItemIndex]["CompanyCode"].ToString();

                try
                {
                    Company.Delete(pkId);

                }
                catch
                {
                    AjaxFuncs.MsgBox(this, "Failed to delete this record, because related data (child records) exists in other tables");
                }

                radGrid.Rebind();
            
        }
        

        protected void radGrid_PreRender(object sender, EventArgs e)
        {
            if (Session["UserRole"].ToString() == "advanced user")
            {
                radGrid.MasterTableView.GetColumn("btnDelete").Display = false;
            }
        }



    }
}